package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.FeriaEvalPart;

public class FeriaEvalPartDAOImpl extends JdbcDaoSupport implements IFeriaEvalPartDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindByIdFeria findByIdFeria;
    
    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM REG_FERIA_EVAL_PART_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(FeriaEvalPart feriaEvalPart) {
		deleteOperation.execute( feriaEvalPart  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public FeriaEvalPart findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(FeriaEvalPart feriaEvalPart) {
		insertOperation.execute( feriaEvalPart );
	}

	public void update(FeriaEvalPart feriaEvalPart) {
		updateOperation.execute( feriaEvalPart );
	}

	public FeriaEvalPart findByCriteria(FeriaEvalPart  feriaEvalPart) {
		return findByCriteriaQuery.runQuery(feriaEvalPart);
	}

	public List load() {
		return findAllQuery.runQuery();
	}
	
	public List loadByIdFeria(FeriaEvalPart feriaEvalPart) {
		return findByIdFeria.runQuery(feriaEvalPart);
	}

	public FeriaEvalPart findByActiveState(FeriaEvalPart  feriaEvalPart) {
		return findByActiveStateQuery.runQuery(feriaEvalPart);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByIdFeria = new FindByIdFeria(getDataSource());
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO REG_FERIA_EVAL_PART_T ("
				  + " ID, "
				  + " ID_FERIA_ARTESANAL, "
				  + " OBSERVACION, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " REG_FERIA_EVAL_PART_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(FeriaEvalPart feriaEvalPart) {
			InsertOperation.this.update( new Object[]{
				feriaEvalPart.getIdFeriaArtesanal(),
				feriaEvalPart.getObservacion(),
				feriaEvalPart.getEstado(),
				feriaEvalPart.getUsuarioCrea(),			
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE REG_FERIA_EVAL_PART_T SET "
					  + " ID_FERIA_ARTESANAL = ? ,"
					  + " OBSERVACION = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
				  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));						
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(FeriaEvalPart feriaEvalPart) {
			UpdateOperation.this.update( new Object[]{
				feriaEvalPart.getIdFeriaArtesanal(),
				feriaEvalPart.getObservacion(),
				feriaEvalPart.getEstado(),
				feriaEvalPart.getUsuarioModifica(),
				feriaEvalPart.getId(),
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_FERIA_EVAL_PART_T "
										+ WHERE + "1=1";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(FeriaEvalPart feriaEvalPart) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_FERIA_EVAL_PART_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			FeriaEvalPart feriaEvalPart = new FeriaEvalPart();
				feriaEvalPart.setId(new Long(rs.getLong("ID")));
				feriaEvalPart.setIdFeriaArtesanal(new Long(rs.getLong("ID_FERIA_ARTESANAL")));
				feriaEvalPart.setObservacion(rs.getString("OBSERVACION"));
				feriaEvalPart.setEstado(rs.getString("ESTADO"));
				feriaEvalPart.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				feriaEvalPart.setFechaCrea(rs.getDate("FECHA_CREA"));		
				feriaEvalPart.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				feriaEvalPart.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return feriaEvalPart;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public FeriaEvalPart runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (FeriaEvalPart) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
			  	  	  					+ " AND ID_FERIA_ARTESANAL = ? "
										+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public FeriaEvalPart runQuery(FeriaEvalPart feriaEvalPart) {
			List result =  execute( new Object[]{
					feriaEvalPart.getIdFeriaArtesanal()
				});
			if ( result.size() > 0 ) {
        		return (FeriaEvalPart) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

 // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByIdFeria extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE 
			//For required criteria elements uncomment required values			  	  	  					
			  	  	  					+ " ID_FERIA_ARTESANAL = ? ";			  	  	  					
    	public FindByIdFeria(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			declareParameter( new SqlParameter(Types.INTEGER) );			
			compile();
    	}
    	public List runQuery(FeriaEvalPart feriaEvalPart) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values				
				feriaEvalPart.getIdFeriaArtesanal()				
				});
			if ( result.size() > 0 ) {
        		return result;
        	} else {
        		return null;
        	}
    	}
    }

    
    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public FeriaEvalPart runQuery(FeriaEvalPart feriaEvalPart) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (FeriaEvalPart) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
